package com.castle.fortress.admin.cms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.cms.dto.CmsArticleTagsDto;
import com.castle.fortress.admin.cms.entity.CmsArticleTagsEntity;
import com.castle.fortress.admin.cms.service.CmsArticleTagsService;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.utils.ExcelUtils;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * cms内容标签管理表 控制器
 *
 * @author castle
 * @since 2022-03-18
 */
@Api(tags="cms内容标签管理表管理控制器")
@Controller
public class CmsArticleTagsController {
    @Autowired
    private CmsArticleTagsService cmsArticleTagsService;

    /**
     * cms内容标签管理表的分页展示
     * @param cmsArticleTagsDto cms内容标签管理表实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("cms内容标签管理表分页展示")
    @GetMapping("/cms/cmsArticleTags/page")
    @ResponseBody
    @RequiresPermissions("cms:cmsArticleTags:pageList")
    public RespBody<IPage<CmsArticleTagsDto>> pageCmsArticleTags(CmsArticleTagsDto cmsArticleTagsDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<CmsArticleTagsDto> page = new Page(pageIndex, pageSize);
        IPage<CmsArticleTagsDto> pages = cmsArticleTagsService.pageCmsArticleTags(page, cmsArticleTagsDto);

        return RespBody.data(pages);
    }

    /**
     * cms内容标签管理表的列表展示
     * @param cmsArticleTagsDto cms内容标签管理表实体类
     * @return
     */
    @ApiOperation("cms内容标签管理表列表展示")
    @GetMapping("/cms/cmsArticleTags/list")
    @ResponseBody
    public RespBody<List<CmsArticleTagsDto>> listCmsArticleTags(CmsArticleTagsDto cmsArticleTagsDto){
        List<CmsArticleTagsDto> list = cmsArticleTagsService.listCmsArticleTags(cmsArticleTagsDto);
        return RespBody.data(list);
    }

    /**
     * cms内容标签管理表保存
     * @param cmsArticleTagsDto cms内容标签管理表实体类
     * @return
     */
    @ApiOperation("cms内容标签管理表保存")
    @PostMapping("/cms/cmsArticleTags/save")
    @ResponseBody
    @RequiresPermissions("cms:cmsArticleTags:save")
    public RespBody<String> saveCmsArticleTags(@RequestBody CmsArticleTagsDto cmsArticleTagsDto){
        if(cmsArticleTagsDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        CmsArticleTagsEntity cmsArticleTagsEntity = ConvertUtil.transformObj(cmsArticleTagsDto,CmsArticleTagsEntity.class);
        if(cmsArticleTagsService.save(cmsArticleTagsEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * cms内容标签管理表编辑
     * @param cmsArticleTagsDto cms内容标签管理表实体类
     * @return
     */
    @ApiOperation("cms内容标签管理表编辑")
    @PostMapping("/cms/cmsArticleTags/edit")
    @ResponseBody
    @RequiresPermissions("cms:cmsArticleTags:edit")
    public RespBody<String> updateCmsArticleTags(@RequestBody CmsArticleTagsDto cmsArticleTagsDto){
        if(cmsArticleTagsDto == null || cmsArticleTagsDto.getId() == null || cmsArticleTagsDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        CmsArticleTagsEntity cmsArticleTagsEntity = ConvertUtil.transformObj(cmsArticleTagsDto,CmsArticleTagsEntity.class);
        if(cmsArticleTagsService.updateById(cmsArticleTagsEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * cms内容标签管理表删除
     * @param id
     * @return
     */
    @ApiOperation("cms内容标签管理表删除")
    @PostMapping("/cms/cmsArticleTags/delete")
    @ResponseBody
    @RequiresPermissions("cms:cmsArticleTags:delete")
    public RespBody<String> deleteCmsArticleTags(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(cmsArticleTagsService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * cms内容标签管理表批量删除
     * @param ids
     * @return
     */
    @ApiOperation("cms内容标签管理表批量删除")
    @PostMapping("/cms/cmsArticleTags/deleteBatch")
    @ResponseBody
    @RequiresPermissions("cms:cmsArticleTags:deleteBatch")
    public RespBody<String> deleteCmsArticleTagsBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(cmsArticleTagsService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * cms内容标签管理表详情
     * @param id cms内容标签管理表id
     * @return
     */
    @ApiOperation("cms内容标签管理表详情")
    @GetMapping("/cms/cmsArticleTags/info")
    @ResponseBody
    @RequiresPermissions("cms:cmsArticleTags:info")
    public RespBody<CmsArticleTagsDto> infoCmsArticleTags(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        CmsArticleTagsEntity cmsArticleTagsEntity = cmsArticleTagsService.getById(id);
        CmsArticleTagsDto cmsArticleTagsDto = ConvertUtil.transformObj(cmsArticleTagsEntity,CmsArticleTagsDto.class);

        return RespBody.data(cmsArticleTagsDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
	@PostMapping("/cms/cmsArticleTags/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<CmsArticleTagsDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<CmsArticleTagsDto> list = cmsArticleTagsService.listCmsArticleTags(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
